package com.mxx.pmxt.controller;

import com.mxx.pmxt.model.GoodsModel;
import com.mxx.pmxt.model.pojo.AfterCheckByUserModel;
import com.mxx.pmxt.service.GoodsService;
import com.mxx.pmxt.service.PUserService;
import com.mxx.pmxt.service.UserGoodsService;
import com.mxx.pmxt.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by 马潇霄 on 2017/3/20.
 */

@Controller
public class ViewController
{
    @Resource
    PUserService pUserService;

    @Resource
    GoodsService goodsService;

    @Resource
    UserGoodsService userGoodsService;


    @RequestMapping("/showLogin")
    public String showLogin()
    {
        return "login";
    }

    @RequestMapping("/showReg")
    public String showReg()
    {
        return "reg";
    }

    @RequestMapping("/showIndex")
    public String showIndex(HttpServletRequest request, Model model)
    {
        HttpSession session = request.getSession();
        String uname = (String) session.getAttribute("uname");
        //Integer utype = (Integer) session.getAttribute("utype");
        model.addAttribute("uName", uname);
        return "index";
    }

    @RequestMapping("/showTestSelectCity")
    public String testSelectCity()
    {
        return "testSelectCity";
    }

    @RequestMapping("/showNavigation")
    public String showNavigation()
    {
        return "navigation";
    }

    @RequestMapping("/showUploadgoods")
    public String showUploadgoods()
    {
        return "uploadgoods";
    }

    @RequestMapping("/showSaveMoney")
    public String showSaveMoney()
    {
        return "savemoney";
    }


    @RequestMapping("/showBeforeCheck")
    public String showBeforeCheck(HttpSession session, Model model, String page)
    {
        String uname = (String) session.getAttribute("uname");
        int beforePageItem = goodsService.countBeforeGoodsByUser(uname);
        final int beforePageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil beforePageUtil = new PageUtil(beforePageItem, beforePageSize, Integer.parseInt(page));
        int beforePageNum = beforePageUtil.getPageNum();
        int beforeStartRow = beforePageUtil.getStartRow();
        List<GoodsModel> beforeGoodsModels = goodsService.getBeforeCheckGoodsByUserByPage(uname, beforeStartRow, beforePageSize);

        model.addAttribute("beforeHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("beforeHasNextPage", (Integer.parseInt(page) != beforePageNum) && (beforePageNum != 0));
        model.addAttribute("beforePrePage", Integer.parseInt(page) - 1);
        model.addAttribute("beforeNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("beforeGoodsModels", beforeGoodsModels);
        model.addAttribute("beforePageItem", beforePageItem);
        model.addAttribute("beforePageNum", beforePageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "beforeCheck";
    }

    @RequestMapping("/showFalseCheck")
    public String showFalseCheck(HttpSession session, Model model, String page)
    {
        String uname = (String) session.getAttribute("uname");
        int falsePageItem = goodsService.countFalseGoodsByUser(uname);
        final int falsePageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil falsePageUtil = new PageUtil(falsePageItem, falsePageSize, Integer.parseInt(page));
        int falsePageNum = falsePageUtil.getPageNum();
        int falseStartRow = falsePageUtil.getStartRow();
        List<GoodsModel> falseGoodsModels = goodsService.getFalseCheckGoodsByUserByPage(uname, falseStartRow, falsePageSize);

        model.addAttribute("falseHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("falseHasNextPage", (Integer.parseInt(page) != falsePageNum) && (falsePageNum != 0));
        model.addAttribute("falsePrePage", Integer.parseInt(page) - 1);
        model.addAttribute("falseNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("falseGoodsModels", falseGoodsModels);
        model.addAttribute("falsePageItem", falsePageItem);
        model.addAttribute("falsePageNum", falsePageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "falseCheck";
    }

    @RequestMapping("/showMyuploadgoods")
    public String showAfterCheck(HttpSession session, Model model, String page)
    {
        String uname = (String) session.getAttribute("uname");
        int afterPageItem = goodsService.countAfterGoodsByUser(uname);
        final int afterPageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil afterPageUtil = new PageUtil(afterPageItem, afterPageSize, Integer.parseInt(page));
        int afterPageNum = afterPageUtil.getPageNum();
        int afterStartRow = afterPageUtil.getStartRow();
        List<AfterCheckByUserModel> afterGoodsModels = goodsService.getAfterCheckGoodsByUserByPage(uname,afterStartRow,afterPageSize);

        model.addAttribute("afterHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("afterHasNextPage", (Integer.parseInt(page) != afterPageNum) && (afterPageNum != 0));
        model.addAttribute("afterPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("afterNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("afterGoodsModels", afterGoodsModels);
        model.addAttribute("afterPageItem", afterPageItem);
        model.addAttribute("afterPageNum", afterPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "myuploadgoods";
    }

    @RequestMapping("/showPmend")
    public String showPmend(HttpSession session, Model model,String page)
    {String uname = (String) session.getAttribute("uname");
        int endPageItem = goodsService.countEndGoodsByUser(uname);
        final int endPageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil endPageUtil = new PageUtil(endPageItem, endPageSize, Integer.parseInt(page));
        int endPageNum = endPageUtil.getPageNum();
        int endStartRow = endPageUtil.getStartRow();
        List<AfterCheckByUserModel> endGoodsModels = goodsService.getEndCheckGoodsByUserByPage(uname,endStartRow,endPageSize);

        model.addAttribute("endHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("endHasNextPage", (Integer.parseInt(page) != endPageNum) && (endPageNum != 0));
        model.addAttribute("endPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("endNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("endGoodsModels", endGoodsModels);
        model.addAttribute("endPageItem", endPageItem);
        model.addAttribute("endPageNum", endPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));

        return "pmend";
    }

    @RequestMapping("/showMybuygoods")
    public String showMybuygoods(HttpSession session, Model model, String page)
    {
        String uname = (String )session.getAttribute("uname");
        int buyPageItem = userGoodsService.countSelectIsGetByUserByPage(uname);
        final int buyPageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil buyPageUtil = new PageUtil(buyPageItem, buyPageSize, Integer.parseInt(page));
        int buyPageNum = buyPageUtil.getPageNum();
        int buyStartRow = buyPageUtil.getStartRow();
        List<AfterCheckByUserModel> buyGoodsModels = userGoodsService.selectIsGetByUserByPage(buyStartRow,buyPageSize,uname);

        model.addAttribute("buyHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("buyHasNextPage", (Integer.parseInt(page) != buyPageNum) && (buyPageNum != 0));
        model.addAttribute("buyPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("buyNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("buyGoodsModels", buyGoodsModels);
        model.addAttribute("buyPageItem", buyPageItem);
        model.addAttribute("buyPageNum", buyPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "mybuygoods";
    }

    @RequestMapping("/showModifyuserinfo")
    public String showModifyuserinfo(Model model, HttpSession session)
    {

        Map<String, String> map = pUserService.showUserinfo((String) session.getAttribute("uname"));
        model.addAttribute("name", map.get("name"));
        model.addAttribute("tel", map.get("tel"));
        model.addAttribute("relname", map.get("relname"));
        model.addAttribute("address1", map.get("address1"));
        model.addAttribute("address2", map.get("address2"));
        model.addAttribute("sex", map.get("sex"));
        return "modifyuserinfo";
    }

    @RequestMapping("/showBookkinds")
    public String showBookkinds(HttpSession session, Model model, String page)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 1)
        {
            return "error";
        }
        int afterPageItem = goodsService.countAfterGoodsByKinds(1);
        final int afterPageSize = 12;
        if (page == null)
        {
            page = "1";
        }
        PageUtil afterPageUtil = new PageUtil(afterPageItem, afterPageSize, Integer.parseInt(page));
        int afterPageNum = afterPageUtil.getPageNum();
        int afterStartRow = afterPageUtil.getStartRow();
        List<GoodsModel> afterGoodsModels = goodsService.getAfterCheckGoodsByPageBykinds(afterStartRow, afterPageSize, 1);

        model.addAttribute("afterHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("afterHasNextPage", (Integer.parseInt(page) != afterPageNum) && (afterPageNum != 0));
        model.addAttribute("afterPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("afterNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("afterGoodsModels", afterGoodsModels);
        model.addAttribute("afterPageItem", afterPageItem);
        model.addAttribute("afterPageNum", afterPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "bookkinds";
    }

    @RequestMapping("/showOtherkinds")
    public String showOtherkinds(HttpSession session, Model model, String page)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 1)
        {
            return "error";
        }
        int afterPageItem = goodsService.countAfterGoodsByKinds(0);
        final int afterPageSize = 12;
        if (page == null)
        {
            page = "1";
        }
        PageUtil afterPageUtil = new PageUtil(afterPageItem, afterPageSize, Integer.parseInt(page));
        int afterPageNum = afterPageUtil.getPageNum();
        int afterStartRow = afterPageUtil.getStartRow();
        List<GoodsModel> afterGoodsModels = goodsService.getAfterCheckGoodsByPageBykinds(afterStartRow, afterPageSize, 0);
        Boolean afterHasNextPage = (Integer.parseInt(page) != afterPageNum) && (afterPageNum != 0);
        model.addAttribute("afterHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("afterHasNextPage", afterHasNextPage);
        model.addAttribute("afterPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("afterNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("afterGoodsModels", afterGoodsModels);
        model.addAttribute("afterPageItem", afterPageItem);
        model.addAttribute("afterPageNum", afterPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "otherkinds";
    }

    @RequestMapping("/showSearchResult")
    public String showSearchResult(HttpSession session, Model model, String page,String gname)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 1)
        {
            return "error";
        }
        int afterPageItem = goodsService.countSelectAfterByName(gname);
        final int afterPageSize = 12;
        if (page == null)
        {
            page = "1";
        }
        PageUtil afterPageUtil = new PageUtil(afterPageItem, afterPageSize, Integer.parseInt(page));
        int afterPageNum = afterPageUtil.getPageNum();
        int afterStartRow = afterPageUtil.getStartRow();
        List<GoodsModel> afterGoodsModels = goodsService.selectAfterByPageByName(gname,afterStartRow,afterPageSize);
        Boolean afterHasNextPage = (Integer.parseInt(page) != afterPageNum) && (afterPageNum != 0);
        model.addAttribute("afterHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("afterHasNextPage", afterHasNextPage);
        model.addAttribute("afterPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("afterNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("afterGoodsModels", afterGoodsModels);
        model.addAttribute("afterPageItem", afterPageItem);
        model.addAttribute("afterPageNum", afterPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        if (afterGoodsModels.size()==0){
            model.addAttribute("size",0);
        }else {
            model.addAttribute("size",1);
        }
        return "searchresult";
    }

    @RequestMapping("/showGoodsDetail")
    public String showGoodsDetail(Integer gid, HttpSession session, Model model)
    {
        GoodsModel goodsModel = goodsService.selectGoodModelByGid(gid);
        model.addAttribute("goods", goodsModel);
        BigDecimal nowPrice = userGoodsService.maxPriceByGid(gid);
        model.addAttribute("nowprice", nowPrice);
        return "goodsdetail";
    }
    //===============================以下是admin部分======================================

    @RequestMapping("/showAdminBeforeCheck")
    public String showAdminBeforeCheck(HttpSession session, Model model, String page)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 3)
        {
            return "error";
        }
        int beforePageItem = goodsService.countBeforeGoods();
        final int beforePageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil beforePageUtil = new PageUtil(beforePageItem, beforePageSize, Integer.parseInt(page));
        int beforePageNum = beforePageUtil.getPageNum();
        int beforeStartRow = beforePageUtil.getStartRow();
        List<GoodsModel> beforeGoodsModels = goodsService.getBeforeCheckGoodsByPage(beforeStartRow, beforePageSize);

        model.addAttribute("beforeHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("beforeHasNextPage", (Integer.parseInt(page) != beforePageNum) && beforePageNum != 0);
        model.addAttribute("beforePrePage", Integer.parseInt(page) - 1);
        model.addAttribute("beforeNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("beforeGoodsModels", beforeGoodsModels);
        model.addAttribute("beforePageItem", beforePageItem);
        model.addAttribute("beforePageNum", beforePageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "admin";
    }

    @RequestMapping("/showAdminAfterCheck")
    public String showAdminAfterCheck(HttpSession session, Model model, String page)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 3)
        {
            return "error";
        }
        int afterPageItem = goodsService.countAfterGoods();
        final int afterPageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil afterPageUtil = new PageUtil(afterPageItem, afterPageSize, Integer.parseInt(page));
        int afterPageNum = afterPageUtil.getPageNum();
        int afterStartRow = afterPageUtil.getStartRow();
        List<GoodsModel> afterGoodsModels = goodsService.getAfterCheckGoodsByPage(afterStartRow, afterPageSize);

        model.addAttribute("afterHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("afterHasNextPage", (Integer.parseInt(page) != afterPageNum) && afterPageNum != 0);
        model.addAttribute("afterPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("afterNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("afterGoodsModels", afterGoodsModels);
        model.addAttribute("afterPageItem", afterPageItem);
        model.addAttribute("afterPageNum", afterPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "adminaftercheck";
    }

    @RequestMapping("/showAdminFalseCheck")
    public String showAdminFalseCheck(HttpSession session, Model model, String page)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 3)
        {
            return "error";
        }
        int falsePageItem = goodsService.countFalseGoods();
        final int falsePageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil falsePageUtil = new PageUtil(falsePageItem, falsePageSize, Integer.parseInt(page));
        int falsePageNum = falsePageUtil.getPageNum();
        int falseStartRow = falsePageUtil.getStartRow();
        List<GoodsModel> falseGoodsModels = goodsService.getFalseCheckGoodsByPage(falseStartRow, falsePageSize);
        Boolean falseHasNextPage = (Integer.parseInt(page) != falsePageNum) && (falsePageNum != 0);
        model.addAttribute("falseHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("falseHasNextPage", falseHasNextPage);
        model.addAttribute("falsePrePage", Integer.parseInt(page) - 1);
        model.addAttribute("falseNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("falseGoodsModels", falseGoodsModels);
        model.addAttribute("falsePageItem", falsePageItem);
        model.addAttribute("falsePageNum", falsePageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "adminfalsecheck";
    }

    @RequestMapping("/showAdminEndGoods")
    public String showAdminEndCheck(HttpSession session, Model model, String page)
    {
        int utype = (Integer) session.getAttribute("utype");
        if (utype != 3)
        {
            return "error";
        }
        int endPageItem = goodsService.countEndGoods();
        final int endPageSize = 5;
        if (page == null)
        {
            page = "1";
        }
        PageUtil endPageUtil = new PageUtil(endPageItem, endPageSize, Integer.parseInt(page));
        int endPageNum = endPageUtil.getPageNum();
        int endStartRow = endPageUtil.getStartRow();
        List<GoodsModel> endGoodsModels = goodsService.getEndCheckGoodsByPage(endStartRow, endPageSize);

        model.addAttribute("endHasPrePage", Integer.parseInt(page) != 1);
        model.addAttribute("endHasNextPage", (Integer.parseInt(page) != endPageNum) && (endPageNum != 0));
        model.addAttribute("endPrePage", Integer.parseInt(page) - 1);
        model.addAttribute("endNextPage", Integer.parseInt(page) + 1);
        model.addAttribute("endGoodsModels", endGoodsModels);
        model.addAttribute("endPageItem", endPageItem);
        model.addAttribute("endPageNum", endPageNum);
        model.addAttribute("nowPage", Integer.parseInt(page));
        return "adminendgoods";
    }
}
